package com.employ.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.employ.admin.common.Result;
import com.employ.admin.entity.User;
import com.employ.admin.mapper.UserMapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;

@Component
@Service
public class UserService {

    @Resource
    UserMapper userMapper;
    public Result<?> insert(@RequestBody User user){
        userMapper.insert(user);

        System.out.println("插入数据成功");
        return Result.success();
    }


    public Result<?> updateById(@RequestBody User user){
        userMapper.updateById(user);

        System.out.println("更新数据成功");
        return Result.success();
    }


    public User getUserByUserName(String userName){
        QueryWrapper queryWrapper = new QueryWrapper();

        if (StringUtils.isNotBlank(userName)){//searchStr不为空，再设置为模
            // 糊查询
            queryWrapper.eq("username",userName);
            User user = userMapper.selectOne(queryWrapper);
            System.out.println("查询到User:"+ userMapper.toString());
            return user;
        }else {
            return new User();
        }

    }

    public User getUserByUserName(String username,String password){
        QueryWrapper queryWrapper = new QueryWrapper();

        if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)){//searchStr不为空，再设置为模
            // 糊查询
            queryWrapper.eq("username",username);
            User user = userMapper.selectOne(queryWrapper);
            System.out.println("查询到User:"+ userMapper.toString());
            return user;
        }else {
            return new User();
        }

    }
}
